package com.igor.bookStore.tapestry.pages;

import org.apache.tapestry5.annotations.Component;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.igor.bookStore.inface.User;
import com.igor.bookStore.service.Users;
import com.igor.bookStore.util.Security;


public class Login {
    private String userName;
    private String password;

    @Component
    private Form loginForm;

    @Inject
    private Messages messages;

    @Inject
    @Property
    private Users users;

    @ApplicationState
    private User user;

    Object onSuccess() {
            return Index.class;
    }

    void onValidateForm() {
            System.out.println("In onValidate");
            User authenticatedUser = Security.authenticate(userName, password,users);
            if (authenticatedUser != null) {
                    user = authenticatedUser;
            } else {
                    System.out.println("authentication-failed");
                    loginForm.recordError(messages.get("authentication-failed"));
            }
    }

    public String getOrRegisterLabel() {
            return messages.get("or-register");
    }
    
    void onSubmitFromLoginForm() {
    	
    }

    public String getUserName() {
            return userName;
    }

    public void setUserName(String userName) {
            this.userName = userName;
    }

    public String getPassword() {
            return password;
    }

    public void setPassword(String password) {
            this.password = password;
    }
}

